package com.example.cartmanage.chewei.view

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.cartmanage.chewei.entity.CheweiEntityItem
import com.example.cartmanage.chewei.model.CheweiViewModel
import com.example.cartmanage.databinding.ActivityDetailCheweiBinding
import com.example.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/cartmanage/DetailCheweiActivity")
class DetailCheweiActivity : BaseActivity<ActivityDetailCheweiBinding, CheweiViewModel>() {

    @Autowired
    @JvmField
    var cheweiid = 0
    lateinit var bean: CheweiEntityItem

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.detailChewei(cheweiid)
        binding.back?.setOnClickListener { finish() }
        setEditable(false)

        binding.edit?.setOnClickListener {
            setEditable(true)
            binding.btnSave?.visibility = android.view.View.VISIBLE
        }
        binding.btnSave?.setOnClickListener {
            // 收集数据并保存
            bean.spaceStatus = binding.tvSpaceStatus.text.toString()
            bean.userName = binding.tvUserName.text.toString()
            bean.licensePlate = binding.tvLicensePlate.text.toString()
            bean.remarks = binding.tvRemarks.text.toString()
            bean.spaceNumber = binding.tvSpaceNumber.text.toString()
            bean.spaceType = binding.tvSpaceType.text.toString()
            bean.areaName = binding.tvAreaName.text.toString()
            setEditable(false)
            binding.btnSave?.visibility = android.view.View.GONE
            vm.editChewei(cheweiid, bean)
        }
    }

    override fun initData() {
        vm.editCheweiSuccess.observe(this) {
         ToastUtils.showShort("保存成功")
        }
        vm.detailcheweiSuccess.observe(this) {
            bean = it
            binding.tvAreaName.setText(it.areaName)
            binding.tvSpaceNumber.setText(it.spaceNumber)
            binding.tvSpaceType.setText(it.spaceType)
            binding.tvLicensePlate.setText(it.licensePlate)
            binding.tvRemarks.setText(it.remarks)
            binding.tvUserName.setText(it.userName)
            binding.tvSpaceStatus.setText(it.spaceStatus)
            setEditable(false)
            binding.btnSave?.visibility = android.view.View.GONE
        }
    }

    private fun setEditable(editable: Boolean) {
        binding.tvSpaceStatus.isEnabled = editable
        binding.tvSpaceStatus.isFocusable = editable
        binding.tvSpaceStatus.isFocusableInTouchMode = editable

        binding.tvUserName.isEnabled = editable
        binding.tvUserName.isFocusable = editable
        binding.tvUserName.isFocusableInTouchMode = editable

        binding.tvLicensePlate.isEnabled = editable
        binding.tvLicensePlate.isFocusable = editable
        binding.tvLicensePlate.isFocusableInTouchMode = editable

        binding.tvRemarks.isEnabled = editable
        binding.tvRemarks.isFocusable = editable
        binding.tvRemarks.isFocusableInTouchMode = editable

        binding.tvSpaceNumber.isEnabled = editable
        binding.tvSpaceNumber.isFocusable = editable
        binding.tvSpaceNumber.isFocusableInTouchMode = editable

        binding.tvSpaceType.isEnabled = editable
        binding.tvSpaceType.isFocusable = editable
        binding.tvSpaceType.isFocusableInTouchMode = editable

        binding.tvAreaName.isEnabled = editable
        binding.tvAreaName.isFocusable = editable
        binding.tvAreaName.isFocusableInTouchMode = editable
    }
}